<?php
/**
 *
 *----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by PhpStorm.
 * User: huangxh
 * Date: 2018/1/8 0008
 * Time: 下午 2:50
 */
use Yaf\Registry;
use ErrorMsg\Admin\ErrorMsg;
use models\DAO\UserModel;

class UserController extends BaseController
{

    public function indexAction(){
        $this->display('index');
    }

    /**
     * 列表数据
     * @throws ReflectionException
     * @throws \Yaf\Exception
     */
    public function dataAction(){
        $user = UserModel::getInstance();
        $condition = [];
        if (!empty(Registry::get('parameters')['searchText'])) $condition = ['mobile' =>Registry::get("parameters")['searchText']];
        $sort = !empty(Registry::get('parameters')['sort'])? Registry::get("parameters")['sort']:'id';

        list($users,$mate) = $user->getList(
            $condition,Registry::get("parameters")['pageNumber'],Registry::get("parameters")['pageSize'],
            [$sort=>Registry::get("parameters")['sortOrder']]
        );
        $this->returnJson(['rows'=>$users,'total'=>$mate],false);
    }

    /**
     * 删除
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function userDelAction(){
        $user = UserModel::getInstance();
        $result = $user->deleteData(Registry::get("parameters")['id']);
        if ($result instanceof Exception) throw $result;
        $this->returnJson($result);
    }

    /**
     * 添加
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function userAddAction()
    {
        $user = UserModel::getInstance();
        $data = [
            'username' => Registry::get("parameters")['username'],
            'mobile' => Registry::get("parameters")['mobile'],
            'stats'  =>Registry::get("parameters")['stats']
        ];
        $result =$user->addData($data);
        if ($result instanceof Exception) throw $result;
        $this->returnJson($result);
    }

    /**
     * 修改
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function userEditAction()
    {
        $user = UserModel::getInstance();
        $condition = [
            'id' => Registry::get("parameters")['id'],
            'username' =>Registry::get("parameters")['username'],
            'mobile' =>Registry::get("parameters")['mobile'],
            'stats'  =>Registry::get("parameters")['stats']
        ];
        $result = $user->updateData($condition);
        if ($result instanceof Exception) throw $result;
        $this->returnJson($result,'',001);
    }

    /**
     * from表单处理函数（未启用）
     */
    public function formDataAction(){

    }

    /**
     * 用户添加验证手机号码是否已被注册
     */
    public function validateAction()
    {
        $user = UserModel::getInstance();
        $retule = $user->findOneBy(['mobile' =>Registry::get("parameters")['mobile']]);
        if (empty($retule)){
            $this->returnJson(true,false);
        }else{
            $this->returnJson(false,false);
        }
    }

    /**
     * EXCEL 导出
     * @throws PHPExcel_Exception
     * @throws PHPExcel_Reader_Exception
     * @throws ReflectionException
     * @throws \Yaf\Exception
     */
    public function exportAction()
    {
        $user = UserModel::getInstance();
        $result = \Common\Common::objToArr($user->select());
        $headTitle = ['编号','用户名','号码','密码','角色','头像','状态','次数','创建','最近登陆'];
        $excel = new \Common\PHPExcelLib();
        $excel->setFileName('abc')->setTitleStype()->setHeadTitl($headTitle)->setSheetIndex(0)->setCellValue($result)->export();
        exit;
    }
}